﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility.Json;
using Utility.Log;
using Utility.Models;
using Utility.Pay.Wxpay;
using Utility;

namespace JYJJ.Web.Pages
{
    public partial class WxPay_notify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LogHelper.WriteLog(DateTime.Now.ToString("yyyyMMdd") + ".log 访问时间:" + DateTime.Now.ToString() + "访问的方法名:WxPay_notify \r\n");

            Pay_WeChat resultNotify = new Pay_WeChat(this);
            resultNotify.ProcessNotify();
        }
    }

    public class Pay_WeChat : Notify
    {
        public Pay_WeChat(Page page) : base(page)
        { }

        public override void ProcessNotify()
        {
            WxPayData notifyData = GetNotifyData();

            //检查支付结果中transaction_id是否存在
            if (!notifyData.IsSet("transaction_id"))
            {
                //若transaction_id不存在，则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "支付结果中微信订单号不存在");
                Log.Error(this.GetType().ToString(), "The Pay result is error : " + res.ToXml());
                page.Response.Write(res.ToXml());
                page.Response.End();
            }

            string transaction_id = notifyData.GetValue("transaction_id").ToString();

            //查询订单，判断订单真实性
            if (!QueryOrder(transaction_id))
            {
                //若订单查询失败，则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "订单查询失败");
                Log.Error(this.GetType().ToString(), "Order query failure : " + res.ToXml());
                page.Response.Write(res.ToXml());
                page.Response.End();
            }
            //查询订单成功
            else
            {
                WeChatPay notify = new WeChatPay();

                notify.transaction_id = transaction_id;
                notify.out_trade_no = notifyData.GetValue("out_trade_no").ToString();
                notify.total_fee = notifyData.GetValue("total_fee").ToString();
                notify.attach = notifyData.GetValue("attach").ToString();
                notify.openid = notifyData.GetValue("openid").ToString();

                model_Donate_Order order = BLL.ServceHelper.ServiceDonate().GetDonateOrderModelByOrderID(notify.out_trade_no);
                if (order == null)
                {
                    string strRet = " 根据订单号(" + notify.out_trade_no + ")查询订单为空";
                    WxPayData res = new WxPayData();
                    res.SetValue("return_code", "FAIL");
                    res.SetValue("return_msg", strRet);
                    Log.Info(this.GetType().ToString(), "fail : " + strRet);
                    page.Response.Write(res.ToXml());
                    page.Response.End();
                }
                else
                {
                    //校验收款账号 金额 是否符合 
                    if (notify.total_fee.ToSafeDecimal() != (order.payMoney * 100))
                    {
                        string strRet = " 订单金额不符合";
                        WxPayData res = new WxPayData();
                        res.SetValue("return_code", "FAIL");
                        res.SetValue("return_msg", strRet);
                        Log.Info(this.GetType().ToString(), "fail : " + strRet);
                        page.Response.Write(res.ToXml());
                        page.Response.End();
                    }
                    else
                    {

                        order.Status = 1;
                        BLL.ServceHelper.ServiceDonate().Save(order);
                        BLL.ServceHelper.ServiceDonate().UpdateDonateMoney(order);
                        WxPayData res = new WxPayData();
                        res.SetValue("return_code", "SUCCESS");
                        res.SetValue("return_msg", "OK");
                        Log.Info(this.GetType().ToString(), "order query success : " + res.ToXml());
                        page.Response.Write(res.ToXml());
                        page.Response.End();
                    }
                }
            }
        }

        //查询订单
        private bool QueryOrder(string transaction_id)
        {
            WxPayData req = new WxPayData();
            req.SetValue("transaction_id", transaction_id);
            WxPayData res = WxPayApi.OrderQuery(req);
            if (res.GetValue("return_code").ToString() == "SUCCESS" &&
                res.GetValue("result_code").ToString() == "SUCCESS")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}